package com.lzw.breezeNovel.webApp.service.impl.common;


import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.model.entity.pojo.user.UserLike;
import com.lzw.breezeNovel.model.entity.pojo.video.ShortVideo;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import com.lzw.breezeNovel.webApp.mapper.user.UserLikeMapper;
import com.lzw.breezeNovel.webApp.mapper.video.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.controller.common
 * @date 2025/3/24
 * @description 异步调用的实现类
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class AsyncService {


    /**
     * 异步更新评论点赞数
     */
    @Autowired
    private CommentLikeMapper commentLikeMapper;
    /**
     * 注入短视频评论
     */
    @Autowired
    private ShortCommentMapper shortCommentMapper;
    /**
     * 注入视频评论
     */
    @Autowired
    private VideoCommentMapper videoCommentMapper;
    /**
     * 注入redis
     */
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 注入短视频mapper
     */
    @Autowired
    private ShortVideoMapper shortVideoMapper;
    /**
     * 注入用户mapper
     */
    @Autowired
    private UserInfoMapper userInfoMapper;
    /**
     * 注入用户点赞表
     */
    @Autowired
    private UserLikeMapper userLikeMapper;

    /**
     * 注入视频点赞表
     */
    @Autowired
    private VideoLikeMapper videoLikeMapper;


    /**
     * 异步更新分享数
     * @param videoId
     */
    @Async
    public void updateShareCountAsync(Long videoId) {
        //设置key
        String shareCountKey = "video:share:" + videoId;
        //获取分享数
        try{
            Integer shareCount = (Integer) redisTemplate.opsForValue().get(shareCountKey);
            //判断是否有数据
            if(shareCount != null){
                //更新数据库
                shortVideoMapper.updateShareCount(videoId,shareCount);
            }
        }catch (Exception e)
        {
            log.error("异步更新分享数失败",e);
        }
    }

    /**
     * 异步更新用户点赞数
     * @param userId
     * @param shortId
     * @param b
     */
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void updateUserLikeCount( Long shortId,Integer type) {
        //根据视频id查询用户信息
        UserInfo userInfo = shortVideoMapper.selectUserInfo(shortId);
        Long like = userInfo.getLike();
        //判断是点赞还是取消点赞
        if(type == 0)
        {
            userInfo.setLike(like+1);
            //更新用户点赞数
            userInfoMapper.updateUserInfo(userInfo);
        }else
        {
            userInfo.setLike(like-1);
            //更新用户点赞数
            userInfoMapper.updateUserInfo(userInfo);
        }
    }


    /**
     * 异步更新用户点赞数
     * @param shortId
     * @param userId
     */
    @Async
    public void addUserLike(Long shortId, Long userId,Integer type) {
        //根据视频id查询用户信息
        UserInfo userInfo = shortVideoMapper.selectUserInfo(shortId);
        //判断是点赞还是取消点赞
        if(type == 0)
        {
           //新增点赞记录
            UserLike userLike = new UserLike();
            userLike.setUserId(userInfo.getUserId());
            userLike.setShortId(shortId);
            userLike.setOtherId(userId);
            userLike.setCreateTime(new Date());
            userLike.setCommentId(0L);
            userLikeMapper.insert(userLike);
        }else
        {
            //删除点赞记录
            userLikeMapper.deleteByCommentIdAndUserIdAndType(shortId,userId);
        }
    }


    /**
     * 异步更新用户点赞数
     * @param videoId
     */
    @Async
    public void updateVideoLikeCountAsync(Long videoId,Long userId) {
        //获取短视频点赞数
        ShortVideo shortVideo = shortVideoMapper.selectVidoe(videoId);
        //判断是否存在
        if(shortVideo.getLikeCount() != 0 )
        {
            //更新用户点赞数
            userInfoMapper.UpdateById(userId, shortVideo.getLikeCount());
            //批量删除视频点赞表里面用户对这个视频的点赞
            videoLikeMapper.deleteVideoLikeByVideoId(videoId);
        }
    }
}
